package com.raven.craft.code.generator.common.util;

import com.google.common.base.CaseFormat;
import lombok.NonNull;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author raven
 * @date 2019-06-05 11:34
 */
public class StringUtil {

    public static String trim(@NonNull String str, String trimPrefix, String trimSuffix) {
        String _name = str;
        if (!StringUtils.isEmpty(trimPrefix) && _name.startsWith(trimPrefix)) {
            _name = _name.substring(trimPrefix.length());
        }
        if (!StringUtils.isEmpty(trimSuffix) && _name.endsWith(trimSuffix)) {
            _name = _name.substring(0, _name.length() - trimSuffix.length());
        }
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, _name);
    }

    public static String toUpperCamelCase(String str) {
        return Arrays.stream(str.split("_")).map(StringUtil::capitalize).collect(Collectors.joining());
    }

    public static String capitalize(String str) {
        if (str != null && str.length() > 0 && Character.isLowerCase(str.charAt(0))) {
            return str.substring(0, 1).toUpperCase() + str.substring(1);
        }
        return str;
    }
}
